package com.zkq.event;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author: ZhangKaiQiang
 * @date: 2021/3/15
 * @time: 16:22
 **/
@Component
public class ContextRefreshedApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

    /**
     * IO容器刷新完毕尚未启动
     * @param contextRefreshedEvent
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("监听到容器刷新事件");
    }


    /**
     * IOC容器已经关闭尚未销毁所有Bean
     * @param closedEvent
     */
    @EventListener
    public void onContextClosedEvent(ContextClosedEvent closedEvent){
        System.out.println("监听到容器关闭事件");
    }


    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
                "com.zkq.event");
        ctx.close();
    }
}
